﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;


public class DAL_tbl_department_master
{
    public DAL_tbl_department_master()
    {

    }



    public List<tbl_department_master> select(int DEPARTMENT_ID)
    {
        List<tbl_department_master> list = new List<tbl_department_master>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select DEPARTMENT_ID,DEPARTMENT_NAME,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_department_master where DEPARTMENT_ID=@DEPARTMENT_ID";
        cmd.Parameters.AddWithValue("@DEPARTMENT_ID", DEPARTMENT_ID);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_department_master()
            {
                DEPARTMENT_ID = Convert.ToInt32(dr["DEPARTMENT_ID"]),
                DEPARTMENT_NAME = dr["DEPARTMENT_NAME"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public List<tbl_department_master> selectAll()
    {
        List<tbl_department_master> list = new List<tbl_department_master>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select DEPARTMENT_ID,DEPARTMENT_NAME,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_department_master Order By DEPARTMENT_ID";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_department_master()
            {
                DEPARTMENT_ID = Convert.ToInt32(dr["DEPARTMENT_ID"]),
                DEPARTMENT_NAME = dr["DEPARTMENT_NAME"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public int insert(String DEPARTMENT_NAME, bool IS_ACTIVE, String CREATED_BY, DateTime CREATED_DATE)
    {
        int i = 0;
        string sql = @"Insert into tbl_department_master(DEPARTMENT_NAME, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@DEPARTMENT_NAME, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@DEPARTMENT_NAME", DEPARTMENT_NAME);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", IS_ACTIVE);
        cmd.Parameters.AddWithValue("@CREATED_BY", CREATED_BY);
        cmd.Parameters.AddWithValue("@CREATED_DATE", CREATED_DATE);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int update(int DEPARTMENT_ID, String DEPARTMENT_NAME, bool IS_ACTIVE, String UPDATED_BY, DateTime UPDATED_DATE)
    {
        int i = 0;
        string sql = @"Update tbl_department_master SET DEPARTMENT_NAME=@DEPARTMENT_NAME, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE DEPARTMENT_ID=@DEPARTMENT_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@DEPARTMENT_ID", DEPARTMENT_ID);
        cmd.Parameters.AddWithValue("@DEPARTMENT_NAME", DEPARTMENT_NAME);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", IS_ACTIVE);
        cmd.Parameters.AddWithValue("@UPDATED_BY", UPDATED_BY);
        cmd.Parameters.AddWithValue("@UPDATED_DATE", UPDATED_DATE);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int delete(int DEPARTMENT_ID)
    {
        int i = 0;
        string sql = "Delete FROM tbl_department_master WHERE DEPARTMENT_ID=@DEPARTMENT_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@DEPARTMENT_ID", DEPARTMENT_ID);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



}